const { controller } = require("../controllers/Controller")

// pages/TradingAreaCenter/setSwiper.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:"添加保存轮播图",
    image:"",
    baseUrl:"",
    jumpList:[
      {
        name:"无",
        id:0
      },{
        name:"店铺爆款",
        id:6
      },{
        name:"一卡通",
        id:7
      },{
        name:"拼拼有米",
        id:8
      },{
        name:"限时抵扣",
        id:9
      },{
        name:"超值礼包",
        id:10
      }
    ],
    ids:"",
    state:0,
    index:0,
    couponList:[],
    giftList:[],
    index2:"",
    index3:"",
    type:""
  },
  //保存轮播图
  save(){
    let value={}
    value.image=this.data.image
    value.state=this.data.state
    value.ids=this.data.ids
    value.shopGroupId=this.data.shopGroupId
    if(value.state==6||value.state==7||value.state==8||value.state==9||value.state==10){
      if(this.data.index2){
        value.name=this.data.list[this.data.index2].name
      }else{
        wx.showToast({
          icon:"none",
          title: '请选择活动',
        })
        return
      }
    }
    controller.saveCouponGiftIndex(value).then(res=>{
      if(res.code==1){
        wx.showToast({
          icon:"none",
          title: '添加成功',
          duration:1500
        })

        setTimeout(function(){
          wx.navigateBack({
            delta: 0,
          })
        },1500)
      }
    })
  },

  update(){
    let value={}
    value.id=this.data.id
    value.image=this.data.image
    value.state=this.data.state
    value.ids=this.data.ids
    value.shopGroupId=this.data.shopGroupId
    if(value.state==6||value.state==7||value.state==8||value.state==9||value.state==10){
      if(this.data.index2){
        value.name=this.data.list[this.data.index2].name
      }else{
        wx.showToast({
          icon:"none",
          title: '请选择活动',
        })
        return
      }
    }
    controller.updateCouponGiftIndex(value).then(res=>{
      if(res.code==1){
        wx.showToast({
          icon:"none",
          title: '修改成功',
          duration:1500
        })

        setTimeout(function(){
          wx.navigateBack({
            delta: 0,
          })
        },1500)
      }
    })
  },

  bindCouponChange(e){
    let index=e.detail.value
    this.setData({
      index2:index,
      ids:this.data.list[index].id
    })
  },


  bindJumpChange(e){
    let index=e.detail.value
    this.setData({
      index:index,
      state:this.data.jumpList[index].id
    })
    if(this.data.state==6){
      this.setData({
        type:2
      })
    }else if(this.data.state==7){
      this.setData({
        type:0
      })
    }else if(this.data.state==8){
      this.setData({
        type:3
      })
    }else if(this.data.state==9){
      this.setData({
        type:4
      })
    }else{
      this.setData({
        type:5
      })
    }
    this.setData({
      index2:""
    })
    this.getList()
  },

  //获取一卡通列表
  getList(){
    let value={}
    value.state=0
    value.type=this.data.type
    value.examineState=1
    value.timeState=1
    controller.getBagPage(value).then(res=>{
      this.setData({
        list:res.data
      })
      for(let i=0;i<res.data.length;i++){
        if(this.data.ids==res.data[i].id){
          this.setData({
            index2:i
          })
        }
      }
    })
  },

  // 图片上传
  uploadImg(){
        let that=this;
        wx.chooseImage({
        count:1,
        success (res) {
            const tempFilePaths = res.tempFilePaths;
            const tempFiles=res.tempFiles;
            if(tempFiles[0].size>1024*1024){
              wx.showToast({
                title: '单张图片大不能超过1MB',
                icon: 'none',
                duration: 2000
              })
              return
            }
          wx.uploadFile({
            url: that.data.baseUrl+'/api/img/uploadFile', 
            filePath: tempFilePaths[0],
            name: 'image',
            formData: {
                'sessionId':wx.getStorageSync("usertoken")
            },
            success (res){
              console.log(res);
              let src=JSON.parse(res.data)
              that.setData({
                image:src.data
              })
            }
          })
        }
      })
  },

  //获取轮播图详情
  getFImgDetail(){
    if(this.data.id){
      controller.getFImgDetail({id:this.data.id,shopGroupId:this.data.shopGroupId}).then(res=>{
        this.setData({
          state:res.data.state,
          ids:res.data.ids||'',
          image:res.data.image
        })
        if(res.data.state==6){
          this.setData({
            index:1,
            type:2
          })
        }else if(res.data.state==7){
          this.setData({
            index:2,
            type:0
          })
        }else if(res.data.state==8){
          this.setData({
            index:3,
            type:3
          })
        }else if(res.data.state==9){
          this.setData({
            index:4,
            type:4
          })
        }else if(res.data.state==10){
          this.setData({
            index:5,
            type:5
          })
        }
        this.getList()
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl:app.globalData.IMG_URL
    })
    if(options.id){
      this.setData({
        id:options.id
      })
    }
    if(options.shopGroupId){
      this.setData({
        shopGroupId:options.shopGroupId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getCouponList()
    // this.getGiftList()
    this.getFImgDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})